package com.dk.smsexpress.server.utils;

public class Formatter {

	public static boolean isValidPhoneNumber(String phoneNumber) {
		int length = phoneNumber.length();
		if ((length < 10) || (length > 12)) {
			return false;
		}
		if ((phoneNumber.charAt(0) != '9')
				&& (phoneNumber.charAt(0) != '8'
						|| phoneNumber.charAt(1) != '4'
						|| phoneNumber.charAt(2) != '9' || length != 11)
				&& (phoneNumber.charAt(0) != '0'
						|| phoneNumber.charAt(1) != '9' || length != 10)
				&& (phoneNumber.charAt(0) != '8'
						|| phoneNumber.charAt(1) != '4' || phoneNumber
						.charAt(2) != '1')
				&& (phoneNumber.charAt(0) != '0'
						|| phoneNumber.charAt(1) != '1' || length != 11)) {
			return false;
		}

		for (int i = 1; i < length; ++i) {
			char curChar = phoneNumber.charAt(i);
			if (curChar < '0' || curChar > '9') {
				return false;
			}
		}
		return true;
	}

	public static String parsePhoneNumber(String phoneNumber) {
		int length = phoneNumber.length();
		if (phoneNumber.charAt(0) == '0' && (length == 10) || (length == 12)) {
			return "84" + phoneNumber.substring(1);
		}
		if ((phoneNumber.charAt(0) == '9' && (length == 9))
				|| (phoneNumber.charAt(0) == '1' && (length == 11))) {
			return "84" + phoneNumber;
		}		
		return phoneNumber;
	}
}
